﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace SAF.Controls
{
    [DefaultEvent("PageChanged")]
    public partial class PageControl : XtraUserControl
    {
        /// <summary>
        /// 页大小
        /// </summary>
        public int PageSize { get; set; }
        /// <summary>
        /// 当前页码
        /// </summary>
        public int CurrentPage { get; private set; }
        /// <summary>
        /// 总页数
        /// </summary>
        public int TotalPages { get; private set; }
        /// <summary>
        /// 总行数
        /// </summary>
        public int TotalRecourds { get; private set; }

        public PageControl()
        {
            InitializeComponent();

            this.CurrentPage = 1;
            this.TotalPages = 1;
            this.TotalRecourds = 0;
            this.PageSize = 100;
        }

        public event EventHandler<PageChangedEventArgs> PageChanged;

        /// <summary>
        /// 执行 PageChanged 事件
        /// </summary>
        private void OnPageChanged(int newPage)
        {
            if (this.PageChanged != null)
            {
                this.PageChanged(this, new PageChangedEventArgs() { NewPage = newPage });
            }
            this.CurrentPage = newPage;
        }

        private void ClearGotoPage()
        {
            this.txtGotoPage.EditValue = string.Empty;
        }

        private void btnFirst_Click(object sender, EventArgs e)
        {
            OnPageChanged(1);
            ClearGotoPage();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            int newPage = 0;
            if (this.CurrentPage > 1)
            {
                newPage = this.CurrentPage - 1;
            }
            else
            {
                newPage = this.CurrentPage;
            }
            OnPageChanged(newPage);
            ClearGotoPage();
        }



        private void btnNext_Click(object sender, EventArgs e)
        {
            int newPage = 0;
            if (this.CurrentPage < this.TotalPages)
            {
                newPage = this.CurrentPage + 1;
            }
            else
            {
                newPage = this.TotalPages;
            }
            OnPageChanged(newPage);
            ClearGotoPage();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            int newPage = this.TotalPages;
            OnPageChanged(newPage);
            ClearGotoPage();
        }

        private void txtGotoPage_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !(Char.IsNumber(e.KeyChar) || e.KeyChar == (char)8);
            if (e.KeyChar == (char)Keys.Enter)
            {
                if (string.IsNullOrEmpty(this.txtGotoPage.Text.Trim()))
                {
                    ClearGotoPage();
                    this.txtGotoPage.Focus();
                    return;
                }
                int newPage = 0;
                int.TryParse(this.txtGotoPage.Text, out newPage);

                if (newPage > this.TotalPages)
                {
                    newPage = this.TotalPages;
                }
                else if (newPage < 1)
                {
                    newPage = 1;
                }
                this.txtGotoPage.EditValue = newPage;
                OnPageChanged(newPage);
            }
        }
    }

    public class PageChangedEventArgs : EventArgs
    {
        public int NewPage { get; set; }
    }
}
